package features

import (
	"context"
	"fmt"
	"gitee.com/chunanyong/zorm"
)

func IsDm(ctx context.Context, dbs *DBS) {
	//1.查询多少张表
	tables := make([]*TableStruct, 0)
	finder := zorm.NewFinder()
	finder.Append(" select  TABLE_NAME,comments TABLE_COMMENT from user_tab_comments  ") //查询当前用户下 表名跟表注释
	err := zorm.Query(ctx, finder, &tables, nil)                                         //
	if err != nil {
		fmt.Errorf("查询数据库异常", err)
		panic(err)
	}

	//2.查询各个表结构
	for _, t := range tables {
		finder2 := zorm.NewFinder()
		finder2.Append("SELECT a.COLUMN_NAME,    a.DATA_TYPE  ,  t.comments FROM  USER_TAB_COLUMNS a left join ALL_COL_COMMENTS t on  t.COLUMN_NAME = a.COLUMN_NAME where  t.TABLE_NAME=? and a.TABLE_NAME=? ", t.TableName, t.TableName)
		maps, err := zorm.QueryMap(ctx, finder2, nil)
		if err != nil {
			fmt.Errorf("查询表字段异常：", err)
		}

		//3.查找各个表主键
		finderPK := zorm.NewFinder()
		finderPK.Append("select cu.column_name from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = ? and au.table_name = ? ", "P", t.TableName)
		pkName := ""
		zorm.QueryRow(ctx, finderPK, &pkName)
		//处理数据
		info := typeConversion(maps, pkName, t, dbs)

		//7.在这里生成具体文件
		GenerateFile(info, dbs)
	}
}
